package com.ltxtiyu.video.api;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.common.DataGridParam;
import com.ltxtiyu.video.service.app.MessageService;
import com.ltxtiyu.video.tool.ParamTool;
import com.ltxtiyu.video.tool.Tool;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RequestMapping("/apiMessage")
@RestController
public class ApiMessage {
    @Reference
    MessageService messageService;

    /**
     * 消息首页
     * @param request
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        DataGridParam dgp = Tool.genDataGridParam(request);
        Integer start = (dgp.getPage() - 1) * dgp.getRows();
        Integer rows = dgp.getRows();
        params.put("start", String.valueOf(start));
        params.put("rows", String.valueOf(rows));
        params.put("userId",userId);
        return messageService.index(params);
    }

    /**
     * 消息列表
     * 目前就点赞和粉丝用
     * @param request
     * @return
     */
    @RequestMapping("/listMessage")
    public String listMessage(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        DataGridParam dgp = Tool.genDataGridParam(request);
        Integer start = (dgp.getPage() - 1) * dgp.getRows();
        Integer rows = dgp.getRows();
        params.put("start", String.valueOf(start));
        params.put("rows", String.valueOf(rows));
        params.put("userId",userId);
        return messageService.listMessage(params);
    }

    /**
     * 已读消息
     * 暂时只是点赞和粉丝
     * "@" 和 评论进首页的时候就已读了
     * @param request
     * @return
     */
    @RequestMapping("/readMessage")
    public String readMessage(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        params.put("userId",userId);
        return messageService.readMessage(params);
    }

    /**
     * 删除消息
     * 目前只有删除评论消息时会用到
     * 删除评论做逻辑删除
     * @param request
     * @return
     */
    @RequestMapping("/deleteMessage")
    public String deleteMessage(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        params.put("userId",userId);
        return messageService.deleteMessage(params);
    }


}
